<?php
    session_start();  
    require '../crud/CustomSecurity.class.php';
    require '../common/Utility.php';
    require '../crud/Users.class.php';
    require '../crud/Userdata.class.php';
    require '../crud/Matches.class.php';
    require '../crud/Prognosis.class.php';
    require '../db/configuration.php';
    loginCheck();
    loadDB();
    isAdmin();
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">

<html lang="bg">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
    <link rel="stylesheet" type="text/css" href="../css/page.css" />
    <link rel="stylesheet" type="text/css" href="../css/main.css" />
    <link rel="stylesheet" type="text/css" href="../css/generalProduct.css" />
    <link rel="stylesheet" type="text/css" href="../css/add-product.css" />

<!--    <script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>-->
<!--  	<script type="text/javascript" src="js/autocomplete/jquery.autocomplete.js"></script>-->
	<title>Резултат</title>
	<meta name="author" content="Dimtiar Giormov">
	
</head>
    <body>
    <?php 
		if (isset($_POST['idm'])){
			$crud = new Matches(con());
			$crud->update($_POST['idm'], $_POST['resA'], $_POST['resB']);
			computePoints($_POST['idm'], $_POST['resA'], $_POST['resB']);
//			echo "<script language=javascript> window.top.location.reload(1); </script>";			
		} 
	?>
        	<div id="add-product">
			<form action="matchdetails.php" name="addproduct" method="post" autocomplete="off">
    			<div id="add-product-left">
    				<ol>
    				<?php
    				$matches = new Matches(con());
    				if (isset($_GET['idm'])){
    					$match = $matches->getObject($_GET['idm']);
    				} else {
    					$match = $matches->getObject($_POST['idm']);
    				}
    				echo "<li>".$match[0][1]."</li>";
    					echo "<li>".$match[0][2]."</li>";
    				?>
    				
    					<li><input class="table-submit" type="submit" value="Save"></li>
    				</ol>
    			</div>  
    			<div id="add-product-right">
    				<ol>
    					<li>    					
    				<input class="table-input" type="text" id="testinput" name="resA">
    					</li>
    					<li>
    					<input class="table-input" type="text" id="testinput1" name="resB">
    					<?php
    					if (isset($_GET['idm'])){
    						echo '<input type="hidden" id="idm" name="idm" value='.$_GET['idm'].'>';	
    					}
    					?>
    					</li>
					</ol>
				</div>
			</form>
		</div>
	
	 <script>
  $(document).ready(function(){
	  $("#testinput").autocomplete('test2.php');
		 setTimeout("$(\"input[name='brand']\").focus();", 100);
	
  });
  </script>
  <?php

  function computePoints($id, $resA, $resB){
  	$prognosis = new Prognosis(con());
  	$pMatches0 = $prognosis->getMatches($id);
  	$pMatches = $pMatches0;
  	
  	foreach ($pMatches as $pMatch){
  		$points = 0;
  		if ($resA == -1){
  			$points = 0;
  		} else if ($pMatch[0] == $resA && $pMatch[1] == $resB){
			$points = 5;  			
  		} else if( ($pMatch[0] == $pMatch[1] && $resA == $resB) ||
  				($pMatch[0] - $pMatch[1] == $resA - $resB)){
  			$points = 4;
  		} else if($pMatch[0] - $pMatch[1] > 0 && $resA - $resB > 0 ||
  					$pMatch[0] - $pMatch[1] < 0 && $resA - $resB < 0){
  			$points = 3;
  		}
//  		if ($points >0){
  			$prognosis->updateScores($pMatch[2], $points);	
//  		}
  		
  	}
  	transferPoints();
  }
  
  function transferPoints(){
  	$users = new Users(con());
  	$ids = $users->getIds();
  	$userdata = new Userdata(con());
  	$progn = new Prognosis(con());
  	foreach($ids as $id){
  		$id1 = $id[0];
  		$points = $progn->getPointsPerId($id1);
  		$userdata->updatePoints($id1, $points[0][0]);
  	}
  }
  ?>
    </body>
</html>